package com.hqyj.onlineExamination.modules.exam.dao;


import com.hqyj.onlineExamination.modules.exam.entity.Paper;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import com.hqyj.onlineExamination.modules.common.entity.SearchBean;
import com.hqyj.onlineExamination.modules.exam.entity.Paper;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
@Repository
public interface PaperDao {

    //通过试卷id查询试卷包含问题
    @Select("SELECT * FROM exam_paper WHERE id = #{id}")
    @Results(id = "questionResult",value = {
            @Result(column = "id",property = "id"),
            @Result(column = "id",property = "questions",
                    javaType = List.class,
                    many = @Many(select = "com.hqyj.onlineExamination.modules.exam.dao."+
                            "QuestionDao.getAllQuestionsByPaperId")
            )
    })
    Paper getAllPaperById(int id);

    @Select("select * from exam_paper where id = #{id}")
    Paper getPaperById(int id);

    @Select("select * from exam_paper")
    List<Paper> getPapers();

    @Select("select * from exam_paper where subject = #{subject}")
    Paper getPaperBySubject(String subject);

    @Insert("insert into exam_paper (create_date, subject, total_score, total_time) "
            + "values (#{createDate}, #{subject}, #{totalScore}, #{totalTime})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    void insertPaper(Paper paper);

    @Update("update exam_paper set subject = #{subject}, total_score = #{totalScore}, "
            + "total_time = #{totalTime} where id = #{id}")
    void updatePaper(Paper paper);

    @Delete("delete from exam_paper where id = #{id}")
    void deletePaperById(int id);

    @Select("<script>"
            + "select * from exam_paper "
            + "<where> "
            + "<if test='keyWord != \"\" and keyWord != null'>"
            + " and (subject like '%${keyWord}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='orderBy != \"\" and orderBy != null'>"
            + " order by ${orderBy} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by id desc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<Paper> getPapersBySearchBean(SearchBean searchBean);

}
